//
// Created by 王乐 on 2022/2/2.
//

#include "LeGroundGameObject.h"

#include "LeRigidBody.h"
#include "LeQuadRenderer.h"

LeGroundGameObject::LeGroundGameObject() {}

LeGroundGameObject::~LeGroundGameObject() {}

void LeGroundGameObject::Initialize()
{
    strName = "MyGround";

    // Transform
    // ---------
    m_Transform.setPosition(m_Position.x, m_Position.y, m_Position.z);
    m_Transform.setScale(m_Scale.x, m_Scale.y, m_Scale.z);

    // RigidBody
    // ---------
    b2BodyDef bodyDef;
    bodyDef.userData.pointer = reinterpret_cast<uintptr_t>(this);
    bodyDef.type = b2_staticBody;
    bodyDef.position.Set(m_Transform.m_Position.x, m_Transform.m_Position.y);
    bodyDef.angle = m_Transform.m_Rotation.z;

    b2FixtureDef fixtureDef;
    b2PolygonShape polygonshape;
    polygonshape.SetAsBox(m_Transform.m_Scale.x / 2, m_Transform.m_Scale.y / 2);
    fixtureDef.shape = &polygonshape;
    fixtureDef.density = 1.0f;
    fixtureDef.friction = 0.0f;
    fixtureDef.restitution = 0.0f;

    LeRigidBody* pRigidBody = new LeRigidBody();
    pRigidBody->initRigidBody(bodyDef, fixtureDef);

    // Renderer
    // --------
    LeQuadRenderer* pRenderer = new LeQuadRenderer();
    pRenderer->initRenderer(this, m_VertexShaderPath, m_FragmentShaderPath);
    pRenderer->addSprite(m_SpritePath);

    // Initialize Components
    initComponents(pRenderer, pRigidBody, nullptr, nullptr);
}

void LeGroundGameObject::InitializeGroundGameObject(const glm::vec3 &pos, const glm::vec3 &scal,
                                                    const char *vertexshaderpath, const char *fragmentshaderpath,
                                                    const char *spritepath)
{
    m_Position = pos;
    m_Scale = scal;
    m_VertexShaderPath = vertexshaderpath;
    m_FragmentShaderPath = fragmentshaderpath;
    m_SpritePath = spritepath;
}
